function [NSF] = setlist(varargin)
% setlist updates the main window list of experiments from the
% current control table associated with that window

global CONTROL
persistent list

if(isempty(CONTROL))
    return;
end
if(nargin == 0) % complete refresh...
    list = {};
    listindices = [1:length(CONTROL)]; % whole list
else
    listindices = getmainselection;
end;

NSF = size(CONTROL); % make sure have current size
htag=findobj('Tag', 'CTL');
switch computer
    case {'MAC', 'MACI'}
        set(htag,'FontName', 'FixedWidth');
        set(htag, 'FontSize', 10);
        set(htag, 'FontWeight', 'bold');
        set(htag, 'Foregroundcolor', [0 0 0]);
        set(htag, 'Backgroundcolor', [0.89 0.89 0.72]);
    otherwise
end;

this_pos = get(htag, 'Value'); % get the current selection
%if(this_pos < 1 | this_pos > NSF)
%    this_pos = 1;
%end;
thistop = get(htag, 'ListboxTop'); % save current position for top of list
if(thistop < 1 || thistop > NSF(2))
    thistop = 1;
end;
% verify the existence of some fields that might not show
fnames=fieldnames(CONTROL);
if(isempty(find(strcmp([fnames], 'Tag'), 1)))
    [CONTROL.Tag] = deal(0);
end
if(isempty(find(strcmp([fnames], 'act'), 1)))
    [CONTROL.act] = deal(0);
end
if(isempty(find(strcmp([fnames], 'inact'), 1)))
    [CONTROL.inact] = deal(0);
end
if(isempty(find(strcmp([fnames], 'FIT'), 1)))
    [CONTROL.FIT] = deal(0);
end
if(isempty(find(strcmp([fnames], 'CellType'), 1)))
    [CONTROL.CellType] = deal('');
end
if(isempty(find(strcmp([fnames], 'Commentary'), 1)))
    [CONTROL.Commentary] = deal('');
end
if(isempty(find(strcmp([fnames], 'Parameters'), 1)))
    [CONTROL.Parameters] = deal('');
end
if(isempty(find(strcmp([fnames], 'E_C'), 1)))
    [CONTROL.E_C] = deal(0);
end
if(isempty(find(strcmp([fnames], 'G1'), 1)))
    [CONTROL.G1] = deal(0);
end
if(isempty(find(strcmp([fnames], 'G2'), 1)))
    [CONTROL.G2] = deal(0);
end;
if(isempty(find(strcmp([fnames], 'fileinpath'), 1)))
    [CONTROL.fileinpath] = deal(0);
end;

for i = listindices
    if(isempty(CONTROL(i).E_C))
        CONTROL(i).E_C = '[]';
    end
    if(isempty(CONTROL(i).G1))
        CONTROL(i).G1 = 0;
    end;
    if(isempty(CONTROL(i).G2))
        CONTROL(i).G2 = 0;
    end;
    if(isempty(CONTROL(i).CellType))
        CONTROL(i).CellType = 'NoID';
    end;


end;

% set field sizes dynamically so we can look nice on the screen

size_div = setfieldwidth('div');
size_pnd = setfieldwidth('pnd');
size_ec = setfieldwidth('E_C');
size_g1 = setfieldwidth('G1');
size_g2 = setfieldwidth('G2');
size_proto = setfieldwidth('protocol');
size_celltype = setfieldwidth('CellType');


for i=listindices
    if(strcmp(CONTROL(i).subtract, 'sub'))
        sub = 'sub';
        subfile =CONTROL(i).linkfile;
        subrecs = sprintf('[%4d-%4d]',CONTROL(i).sub_frec, CONTROL(i).sub_lrec);
    else
        sub = 'non';
        solution = ''; subfile = ''; subrecs = '';
    end
    if(isempty(CONTROL(i).Tag))
        tg=' ';
    elseif CONTROL(i).Tag==1
        tg='*';
    else
        tg=' ';
    end
    if(isempty(find(strcmp([fnames], 'reclist'), 1)))
        CONTROL(i).reclist=[];
    end;
    if(isempty(CONTROL(i).reclist))
        rlist=sprintf('[%4d-%4d]', CONTROL(i).recbeg, CONTROL(i).recend); % use basic record list
    else
        rl=record_parse(CONTROL(i).reclist);
        a=min(rl);
        b=max(rl);
        rlist=sprintf('[%4d..%4d]', a, b);
    end;
    if(CONTROL(i).fileinpath)
        inpath = ')';
    else
        inpath = '#';
    end;
    if(isnumeric(CONTROL(i).E_C))
        ec = num2str(CONTROL(i).E_C);
    else
        ec = CONTROL(i).E_C;
    end;

    convertlist = {'pnd', 'div', 'G1', 'G2'}; % elements to convert to strings.
    for k = 1:length(convertlist)
        eval(sprintf('%s = convert(CONTROL(i), convertlist{k});', convertlist{k}));
    end;
    list{i} = sprintf('%1c%4d%c %15s  %12s %2s  %*s    %*s  %*s  %*s  %*s  %*s   %3s', ...
        tg, i, inpath, CONTROL(i).filename, rlist, CONTROL(i).mode, 12, CONTROL(i).protocol, ...
        size_pnd, pnd, size_div, div, size_ec, ec, size_g1, G1, size_g2, G2, ...
        CONTROL(i).solution);
    if(strcmp(sub, 'non'))
        list{i}=strcat(list{i}, sprintf('  %*s  %s :: %s', size_celltype, CONTROL(i).CellType, CONTROL(i).Commentary, ...
            CONTROL(i).Parameters));
    else
        list{i}=strcat(list{i}, sprintf(' [%3s] %9s %12s %s', sub, subfile, subrecs));
    end;
    %   list = strvcat(list, nextlist);
end
set(htag, 'Max', NSF(2));
set(htag, 'String', list);
set(htag, 'ListboxTop', thistop); % restore list position after update
if(this_pos > NSF(2))
    this_pos = 1; % be sure the position is allowed.
    set(htag, 'Value', this_pos);
else
    set(htag, 'Value', this_pos);
end;


return;

function [cvt] = convert(C, var) %#ok<INUSL,DEFNU>
%
w = eval([sprintf('C.%s', var)]);
if(ischar(w))
    cvt = w;
else
    cvt = num2str(w);
end;

function fw = setfieldwidth(arg)
global CONTROL %#ok<NUSED>
s = eval(sprintf('[CONTROL.%s]', arg));
if(ischar(s)) % for character strings, simple...
    g2s = eval(sprintf('strvcat(CONTROL.%s)', arg)); % reevaluate as string.
    fw = size(g2s, 2);
else
    mx = max([s]);
    mn = min([s]);
    if(mx == mn)
        fw = 1;
    elseif(mx > 0 && mn >= 0) % all positive
        fw = ceil(log10(mx));
    elseif(mn < 0 && mx <= 0) % all negative
        fw = 1+ceil(log10(-mn));
    else % spans positive and negative
        fw = 1+max(ceil(log10(mx)), ceil(log10(-mn)));
    end;

end;


